package com.toe.forum.webapi.timer.job;

import com.toe.forum.service.ForumArticleService;
import com.toe.forum.webapi.redisRepository.IArticleRedisRepository;
import com.toe.pojo.vo.forum.ArticleDetailVO;
import com.toe.pojo.vo.forum.ArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public class InitialJob implements Job {
    @Autowired
    private ForumArticleService forumArticleService;

    // 查询出来的数据要保存到Redis中
    @Autowired
    private IArticleRedisRepository redisRepository;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("清楚以前的老缓存");
        redisRepository.deleteAllItem();
        log.info("存入所有类别中综合排名前50的文章的详细数据");
        List<ArticleDetailVO> articleDetailVOS = forumArticleService.selectAllArticlePage(1, 50);
        ArticleVO articleVO = new ArticleVO();
        for (ArticleDetailVO articleDetailVO:articleDetailVOS
             ) {
            BeanUtils.copyProperties(articleDetailVO,articleVO);
            redisRepository.save(articleVO);
        }
        log.info("所有类别中综合排名前50的文章的详细数据已经存入完成.");
        List<ArticleDetailVO> articleDetailVOS1 = forumArticleService.selectAllArticle();
        for (ArticleDetailVO articleDetailVO:articleDetailVOS1
        ) {
            redisRepository.saveArticleLike(articleDetailVO.getId(),articleDetailVO.getLikeCount());
            redisRepository.saveArticleEnshrine(articleDetailVO.getId(),articleDetailVO.getEnshrineCount());
            redisRepository.savaArticleLook(articleDetailVO.getId(),articleDetailVO.getPageView());
        }
        log.info("所有文章的点赞数,收藏数已经加入缓存!!!!!!!!");
    }
}
